package com.Jano.config;

import com.Jano.interceptor.CrossDomainInterceptor;
import com.Jano.interceptor.JwtTokenInterceptor;
import com.Jano.interceptor.PermissionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

/**
 * description: MyInterceptors <br>
 * date: 2022/7/12 12:09 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */

/**
 * 一定要加上这个注解，成为Springboot的配置类，不然不会生效
 */
@Configuration
public class MyInterceptors implements WebMvcConfigurer {
    @Autowired
    CrossDomainInterceptor crossDomainInterceptor;
    @Autowired
    JwtTokenInterceptor jwtTokenInterceptor;
    @Autowired
    PermissionInterceptor permissionInterceptor;

    /**
     * 这个方法用来注册拦截器，我们自己写好的拦截器需要通过这里添加注册才能生效
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //跨域
        registry.addInterceptor(crossDomainInterceptor).addPathPatterns("/**");
        //检查是否携带accessToken 现在是除了登陆注册页面 都需要被拦截 后期可以根据情况来改变
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login.do")
                .excludePathPatterns("/jwt/refreshToken");
        //权限判断拦截器
        registry.addInterceptor(permissionInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login.do")
                .excludePathPatterns("/jwt/refreshToken");;
    }

    /**
     * 这个方法是用来配置静态资源的，比如html，js，css，等等
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    /**
     * 添加格式转化器(String->Object)
     *
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        WebMvcConfigurer.super.addFormatters(registry);
    }

    /**
     * 解决跨域问题
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        WebMvcConfigurer.super.addCorsMappings(registry);
    }

    /**
     * 视图跳转控制器
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        WebMvcConfigurer.super.addViewControllers(registry);
    }

    /**
     * 添加参数处理器
     *
     * @param resolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        WebMvcConfigurer.super.addArgumentResolvers(resolvers);
    }

    /**
     * 添加返回值处理器
     *
     * @param handlers
     */
    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
        WebMvcConfigurer.super.addReturnValueHandlers(handlers);
    }


}
